package com.lysh.aliHttp.utils;

import java.util.List;

/**
 * @author liyishan
 * @date 2023/5/5 09:46
 * @apiNote
 */

public class CodingUtils {
    public CodingUtils() {
    }

    public static void assertParameterNotNull(Object param, String paramName) {
        if (param == null) {
            throw new NullPointerException(OSSUtils.COMMON_RESOURCE_MANAGER.getFormattedString("ParameterIsNull", new Object[]{paramName}));
        }
    }

    public static void assertParameterInRange(long param, long lower, long upper) {
        if (!checkParamRange(param, lower, true, upper, true)) {
            throw new IllegalArgumentException(String.format("%d not in valid range [%d, %d]", param, lower, upper));
        }
    }

    public static void assertStringNotNullOrEmpty(String param, String paramName) {
        assertParameterNotNull(param, paramName);
        if (param.trim().length() == 0) {
            throw new IllegalArgumentException(OSSUtils.COMMON_RESOURCE_MANAGER.getFormattedString("ParameterStringIsEmpty", new Object[]{paramName}));
        }
    }

    public static void assertListNotNullOrEmpty(List<?> param, String paramName) {
        assertParameterNotNull(param, paramName);
        if (param.size() == 0) {
            throw new IllegalArgumentException(OSSUtils.COMMON_RESOURCE_MANAGER.getFormattedString("ParameterListIsEmpty", new Object[]{paramName}));
        }
    }

    public static boolean isNullOrEmpty(String value) {
        return value == null || value.length() == 0;
    }

    public static void assertTrue(boolean condition, String message) {
        if (!condition) {
            throw new IllegalArgumentException(message);
        }
    }

    public static boolean checkParamRange(long param, long from, boolean leftInclusive, long to, boolean rightInclusive) {
        if (leftInclusive && rightInclusive) {
            return from <= param && param <= to;
        } else if (leftInclusive && !rightInclusive) {
            return from <= param && param < to;
        } else if (!leftInclusive && !rightInclusive) {
            return from < param && param < to;
        } else {
            return from < param && param <= to;
        }
    }
}
